package net.oschina.app.adapter.me.messageAdapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.base.BaseListAdapter;
import net.oschina.app.bean2.ActiveBean;
import net.oschina.app.ui.activity.DetailActivity;

import static net.oschina.app.base.Global.mContext;

/**
 * Created by Shinelon on 2017/1/15.
 */

public class AtMeListAdapter extends BaseListAdapter<ActiveBean> {
    private String mUrl;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_me_message_atme, parent, false);
        AtMeHolder holder = new AtMeHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ActiveBean data = mList.get(position);
        ((AtMeHolder) holder).tvAuthor.setText(data.getAuthor());
        ((AtMeHolder) holder).tvPubDate.setText(data.getPubDate());
//        ((AtMeHolder)holder).tvMessage.setText(data.getMessage());
        String message = data.getMessage();
        //富文本的显示
        Spanned html = Html.fromHtml(message);
        ((AtMeHolder) holder).tvMessage.setText(html);

        ((AtMeHolder) holder).tvMessage.setMovementMethod(LinkMovementMethod.getInstance());
        /*((AtMeHolder)holder).tvMessage.setFocusable(true);
        // ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setDispatchToParent();
        ((AtMeHolder)holder).tvMessage.setLongClickable(true);*/
        mUrl = data.getUrl();
    }

    class AtMeHolder extends RecyclerView.ViewHolder {
        public TextView tvAuthor;
        public TextView tvPubDate;
        public TextView tvMessage;
        public TextView tvData;

        public AtMeHolder(View itemView) {
            super(itemView);
            tvAuthor = (TextView) itemView.findViewById(R.id.tv_at_author);
            tvPubDate = (TextView) itemView.findViewById(R.id.tv_at_pubDate);
            tvMessage = (TextView) itemView.findViewById(R.id.tv_at_message);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, DetailActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("url", mUrl);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    /*@Override
    protected void bindData() {

        //用户图片
        Picasso.with(mContext).load(beanData.getPortrait()).into(mMoveItemIv);
        //标题
        mMoveItemTitle.setText(beanData.getAuthor());
        //内容
        mMoveItemContent.setText(Html.fromHtml(beanData.getBody()));

        //时间2016-12-27 14:49:27
        String time = beanData.getPubDate().substring(0, 9);
        mMoveItemTime.setText(time);
        //手机
        mMoveItemPhone.setText("Android");
        //评论条数
        mMoveItemCommentNum.setText(beanData.getCommentCount() + "");
        //点赞
        mLikeUser = beanData.getLikeUser();

        if (beanData.getLikeCount() == 0) {
            mMoveItemLikecontent.setVisibility(View.GONE);
        } else {
            mMoveItemLikecontent.setVisibility(View.VISIBLE);



            mMoveUserCentreString = new SpannableStringBuilder();
            String likeSize = mLikeUser.size()+"人";
            for (int i = 0; i < mLikeUser.size(); i++) {
                final User user = mLikeUser.get(i);
                //用户名字
                SpannableStringBuilder spannableString = new SpannableStringBuilder(user.getName());
                //用户集合大小
                SpannableStringBuilder likeUsers = new SpannableStringBuilder(likeSize);
                spannableString.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        Intent intent = new Intent(mContext, MoveUserCentreActivity.class);
                        intent.putExtra("USER", user);
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        ds.setColor(Color.parseColor("#3DB8E5"));
                        ds.setUnderlineText(false);    //去除超链接的下划线

                    }
                },0,user.getName().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


                //跳转到用户中心
                likeUsers.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        Intent intent = new Intent(mContext, MoveLikeUsersActivity.class);
                        intent.putExtra("likeUser", (Serializable) mLikeUser);
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        ds.setColor(Color.parseColor("#3DB8E5"));
                        ds.setUnderlineText(false);    //去除超链接的下划线

                    }
                },0,likeSize.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


                if (i == mLikeUser.size() - 1) {
                    mMoveUserCentreString.append(spannableString);
                    mMoveUserCentreString.append(Html.fromHtml("<font  color=#000000>等</font>"));
                    mMoveUserCentreString.append(likeUsers);
                    mMoveUserCentreString.append(Html.fromHtml("<font  color=#000000>觉得很赞</font>"));
                } else {
                    mMoveUserCentreString.append(spannableString);
                    mMoveUserCentreString.append(Html.fromHtml("<font  color=#000000>、</font>"));
                }
                mMoveItemLikename.setText(mMoveUserCentreString);
                mMoveItemLikename.setMovementMethod(LinkMovementMethod.getInstance());

            }

        }*/
}